package com.hhh.petadopt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hhh.petadopt.pojo.Sentp;
import com.hhh.petadopt.pojo.SentpVo;
import com.hhh.petadopt.service.adminSentService;
import com.hhh.petadopt.utils.JsonUtils;
import com.hhh.petadopt.utils.MailUtils;
import com.hhh.petadopt.utils.Page;

/**
 * 送养管理
 * @author 杜镇声声
 *
 */
@Controller
public class adminSentController {
	//注入
	@Autowired
	private adminSentService adminsentservice;
	
	//显示送养管理页面
	@RequestMapping(value="/adminSsent/showSent.html")
	public String showSent(SentpVo Vo,Model model) {
		//执行查询
		Page<Sentp> page = adminsentservice.selectSents(Vo);
		//将页面信息保存到域中
		model.addAttribute("page", page);
		model.addAttribute("total", page.getTotal());
		//搜索信息的回显
		model.addAttribute("ssname", Vo.getSsname());
		model.addAttribute("ssage", Vo.getSsage());
		model.addAttribute("ssdate", Vo.getSsdate());
		model.addAttribute("sssex", Vo.getSssex());
		return "admin/sent";
	}
	
	//详情页面回显
	@RequestMapping(value="/adminSent/selectSent.html")
	@ResponseBody
	public String selectsentt(Integer id) {
		//执行查询
		Sentp sent = adminsentservice.selectSentpById(id);
		sent.setCreated(sent.getCreated().substring(0, 10));
		System.out.println(sent.getCreated());
		//将数据转换为json格式
		String objectToJson = JsonUtils.objectToJson(sent);
		
		return objectToJson;
	}
	//送养入库
	@RequestMapping(value="/adminSent/storagepet.html")
	@ResponseBody
	public String storagepet(Sentp sentp) {
		adminsentservice.savePett(sentp);
		
		//发邮件
		String emailMsg = sentp.getSman()+",您好! 当你收到此通知时,您送养的宠物"+sentp.getSname()+",编号:"+sentp.getId()+"已经被接收。"
						+"如有疑问请电话咨询:"+"123456";
		try {
			MailUtils.sendMail(sentp.getSemail(), emailMsg);
			} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		return "ok";
	}
	//根据id删除送养信息
	@RequestMapping(value="/adminSent/deleteSent.html")
	@ResponseBody
	public String deleteSent(Integer id) {
		//执行删除作
		adminsentservice.deleteSentById(id);
		return "ok";
	}
	
	//多选删除
	@RequestMapping(value="/adminsent/delsents.html")
	@ResponseBody
	public String deleteSentss(Integer[] ids) {
		//执行操作
		adminsentservice.deleteSentsByIds(ids);
		return "ok";
	}

}
